package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.teaching.mapper.ClassesMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.StudentMapper;
import com.ruoyi.teaching.domain.Student;
import com.ruoyi.teaching.service.IStudentService;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-30
 */
@Service
public class StudentServiceImpl implements IStudentService
{
    public static final long DEFAULT_STUDENT_ROLE_ID = 3L;
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private ClassesMapper classesMapper;
    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        /*HashMap<Object, Object> map = new HashMap<>();

        if (StringUtils.isNotEmpty(student.getName())) {
             map.put("name", student.getName());
        }

        studentMapper.selectByMap();*/
        /*QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(student.getName()),"name", student.getName());*/
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>();
        queryWrapper.eq(StringUtils.isNotEmpty(student.getName()),Student::getName, student.getName());
        return studentMapper.selectList(queryWrapper);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    @GlobalTransactional
    public int insertStudent(Student student)
    {
        //需要为该学生申请一个账号(user id)
        SysUser sysUser = new SysUser();
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setCreateTime(new Date());
        sysUser.setRoleId(DEFAULT_STUDENT_ROLE_ID);
        sysUser.setPhonenumber(student.getContactNumber());
        sysUser.setUserName(student.getUsername());
        sysUser.setNickName(student.getName());
        Long userId = remoteUserService.registerUserInfoForStu(sysUser,"inner").getData();
        if (ObjectUtils.isEmpty(userId)) {
            throw new ServiceException("用户名已经存在");
        }
        //维护和学生表关系
        student.setId(userId);
        int i = 10/0;
        //维护冗余字段
        student.setClassesName(classesMapper.selectClassesById(student.getClassesId()).getClassName());
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(Long[] ids)
    {
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id)
    {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public List<Student> selectStudentListByClassesId(Long classesId) {
        return studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getClassesId,classesId));
    }
}
